/**
 * @author haniotak
 */
package net.es.weathermap {
    public class Bezier {

        public static function getBezier(p0:Vector2D, c1:Vector2D, c2:Vector2D, p3:Vector2D, div:Number, segment:String):Array {
            if (div > 0) {
                var c1_0:Vector2D = Vector2D.interpolate(p0, c1, 0.5);
                var n0:Vector2D = Vector2D.interpolate(c1, c2, 0.5);
                var c2_1:Vector2D = Vector2D.interpolate(c2, p3, 0.5);
                var c1_1:Vector2D = Vector2D.interpolate(c1_0, n0, 0.5);
                var c2_0:Vector2D = Vector2D.interpolate(n0, c2_1, 0.5);
                var p03:Vector2D = Vector2D.interpolate(c1_1, c2_0, 0.5);
                var curveSegment:Array = new Array();
                var curve1:Array = new Array();
                var curve2:Array = new Array();
                if (segment == "first-half" || segment == "whole") {
                    curve1 = Bezier.getBezier(p0, c1_0, c1_1, p03, div-1, "whole");
                    curveSegment = curve1;
                }
                if (segment == "second-half" || segment == "whole") {
                    curve2 = Bezier.getBezier(p03, c2_0, c2_1, p3, div-1, "whole");
                    curveSegment = curve2;
                }
                if (segment == "whole") {
                    curve1.pop();
                    curveSegment = curve1.concat(curve2);

                }

            } else {
                curveSegment = new Array(p0, c1, c2, p3);
            }
           return curveSegment;
        }

        public static function getControlPoints(src:Vector2D, dst:Vector2D, magScale:Number, rotation:Number):Array {
            var dirX:Number = dst.getx() - src.getx();
            var dirY:Number = dst.gety() - src.gety();
            var mag:Number = Math.sqrt(dirX*dirX + dirY*dirY);
            if (mag == 0) {
                 return new Array(dst, src);
            }
            var length:Number = mag*magScale;
            dirX = dirX / mag;
            dirY = dirY / mag;
            var angle:Number = rotation;
            var sin:Number = Math.sin(angle);
            var cos:Number = Math.cos(angle);
            var rotX:Number = cos * dirX - sin * dirY; //x' = cos(t)*x - sin(t)*y
            var rotY:Number = sin * dirX + cos * dirY; //y' = sin(t)*x + cos(t)*y
            var rotNegX:Number = cos * -dirX - sin * dirY; //x' = cos(-t)*-x - sin(-t)*-y
            var rotNegY:Number = sin * dirX - cos * dirY; //y' = sin(-t)*-x + cos(-t)*-y

            // Get one leg of the arrow
            var x1:Number = rotX*length + src.getx();
            var y1:Number = rotY*length + src.gety();

            // Get parallel leg of arrow
            var x2:Number = rotNegX*length + dst.getx();
            var y2:Number = rotNegY*length + dst.gety();

            if(dirX < 0 || (dirX == 0 && dirY < 0)){
                x2 = -rotX*length+ dst.getx();
                y2 = -rotY*length + dst.gety();
                x1 = -rotNegX*length + src.getx();
                y1 = -rotNegY*length + src.gety();
            }

            return new Array(new Vector2D(x2,y2), new Vector2D(x1,y1));
        }
    }

}
